*     *****************************************************************  * 
*     *****************************************************************  * 
*       File-Name:      parties.do                                       *  
*       Date:           1 June 2016                                      * 
*       Author:         Gschwend                                         * 
*       Purpose:      	prepare parties data for merge					 *
* 	    Input Files:    parties.dta                                      *
*       Data Output:    parties_merge.dta                                                 *              
*     ****************************************************************   * 
*     ****************************************************************   * 



version 14.2
clear
capture log close
set more off



**********************************************
* Specify your own local path to the dropbox *
**********************************************


* Thomas G.'s local (office) path:
* local path /Users/gschwend/Dropbox/Andre/OUP multilevel electoral behavior book/chapter 6/replication/


* Thomas G.'s local (Air) path:
* local path /Users/thomasgschwend/Dropbox/Andre/OUP multilevel electoral behavior book/chapter 6/replication/
 

use "parties.dta", clear


* correct mistakes in eid code
gen str geo = country + level + elecid 
tab geo eid

recode eid 141=101 142=102

capture drop if eid==.


/* generate election-specific id
gen eid =.
replace eid = 111 if ELECID==5  //France, National, IDF (Paris)
replace eid = 112 if ELECID==6  //France, National, Provence
replace eid = 101 if ELECID==22 //France, local, IDF (Paris)
replace eid = 102 if ELECID==23 //France, local, Provence (Marseille)
replace eid = 131 if ELECID==17 //France, Europe, IDF (Paris)
replace eid = 132 if ELECID==16 //France, Europe, Provence

replace eid = 211 if ELECID==10 //Germany, National, Lower Sax
replace eid = 212 if ELECID==12  //Germany, National, Bavaria
replace eid = 201 if ELECID==11 //Germany, regioal, Lower Sax
replace eid = 202 if ELECID==13 //Germany, regional, Bavaria
replace eid = 231 if ELECID==18 //Germany, Europe, Lower Sax
replace eid = 232 if ELECID==19 //Germany, Europe, Bavaria

replace eid = 311 if ELECID==7  //Spain, National, Catalonia
replace eid = 312 if ELECID==9  //Spain, National, Madrid
replace eid = 301 if ELECID==8  //Spain, regional, Catalonia
replace eid = 302 if ELECID==24  //Spain, regional, Madrid
replace eid = 331 if ELECID==20 //Spain, Europe, Catalonia
replace eid = 332 if ELECID==21 //Spain, Europe, Madrid
*/

drop country level elecid
compress
*create id to merge in party information for some elections
gen id = eid*100 + partynumber
sort id

save "parties_merge.dta", replace

preserve
keep id large_party

gen id1 = id
gen id2 = id
gen id3 = id
gen id4 = id
gen id5 = id
gen id6 = id
gen id7 = id
gen id8 = id
gen id9 = id

ren large_party largeparty
sort id
save "large_party.dta", replace
restore

* replicate that for most preferred and 2nd most preferred party

preserve
ren id idMP1 
ren seat_current       seat_currentMP1 
ren partynumber        partynumberMP1        
ren incumbent_regional incumbent_regionalMP1 
ren incumbent_national incumbent_nationalMP1 
ren partyname          partynameMP1          
ren incumbent          incumbentMP1          
ren seat_previous      seat_previousMP1       
ren cabinet_current    cabinet_currentMP1     
ren cabinet_previous   cabinet_previousMP1     
ren large_party        large_partyMP1        


keep *MP1
compress
sort idMP1
save "parties_mergeidMP1.dta", replace
restore

preserve
ren id idMP2 
ren seat_current seat_currentMP2 
ren partynumber        partynumberMP2        
ren incumbent_national incumbent_nationalMP2 
ren partyname          partynameMP2          
ren incumbent          incumbentMP2          
ren seat_previous      seat_previousMP2       
ren cabinet_current    cabinet_currentMP2     
ren cabinet_previous   cabinet_previousMP2     
ren large_party        large_partyMP2        


keep *MP2
compress
sort idMP2
save "parties_mergeidMP2.dta", replace
restore

** most preferred large party
